import 'package:ecy_app_flutter/routes/goods/model/EcyGoodsLabelListModel.dart';
import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';

import '../model/EcyGoodsLabelModel.dart';

class EcyGoodsLabelListView extends StatefulWidget {
  final int currentIndex;
  late _EcyGoodsLabelListViewState currentState;
  final ValueChanged<int> onSelectNewValue;
  @override
  State<StatefulWidget> createState() {
    currentState = _EcyGoodsLabelListViewState(currentIndex, onSelectNewValue);
    return currentState;
  }

  EcyGoodsLabelListView(this.currentIndex, this.onSelectNewValue);

  onCurrentIndexChange(int page) {
    currentState.onCurrentIndexChange(page);
  }

}

class _EcyGoodsLabelListViewState extends State {
  int currentIndex = 0;
  final ValueChanged<int> onSelectNewValue;

  List<String> headList = ["首页", "盲抽", "上新"];

  _EcyGoodsLabelListViewState(this.currentIndex, this.onSelectNewValue);

  onCurrentIndexChange(int page) {
    setState(() {
      currentIndex = page;
    });
  }

  @override
  Widget build(BuildContext context) {
    var _this = this;
    return ListView.builder(
        shrinkWrap: true,
        physics: const NeverScrollableScrollPhysics(),
        scrollDirection: Axis.horizontal, // 设置滚动方向为水平
        itemCount: headList.length,
        itemBuilder: (context, index) {
          var item = headList[index];
          return GestureDetector(
              child: Container(
                alignment: Alignment.center,
                margin: EdgeInsets.only(left: 20.w),
                child: currentIndex != index
                    ? Text(
                  item,
                  style: TextStyle(
                      fontFamily: "PingFang SC",
                      fontWeight: FontWeight.w700,
                      fontSize: 16.sp,
                      color: const Color(0xFFBFBFBF)),
                )
                    : Column(
                  mainAxisAlignment: MainAxisAlignment.center,
                  crossAxisAlignment: CrossAxisAlignment.center,
                  children: [
                    Text(
                      item,
                      style: TextStyle(fontFamily: "PingFang SC",
                          fontWeight: FontWeight.w700,
                          fontSize: 18.sp,
                          color: const Color(0xFF000000)),
                    ),
                    Image(
                        image: const AssetImage(
                            'assets/images/ic_home_label_index.png'),
                        width: 34.w,
                        height: 4.w),
                  ],
                ),
              ),
              onTap: () {
                if (_this.onSelectNewValue != null) {
                  _this.onSelectNewValue(index);
                }
                _this.setState(() {
                  currentIndex = index;
                });
              });
        });
  }

  @override
  void dispose() {
    // 清理控制器，避免内存泄漏
    super.dispose();
  }
}
